package org.sub.unit.benchmark.test;


import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.Throughput)//基准测试类型
@OutputTimeUnit(TimeUnit.SECONDS)//基准测试结果的时间类型
@Warmup(iterations = 3)//预热的迭代次数
@Threads(2)//测试线程数量
@State(Scope.Thread)//该状态为每个线程独享
//度量:iterations进行测试的轮次，time每轮进行的时长，timeUnit时长单位,batchSize批次数量
@Measurement(iterations = 2, time = -1, timeUnit = TimeUnit.SECONDS, batchSize = -1)
public class MathematicalBenchMark {

    private final byte[] srcBytes = {83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 97, 98, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 44, 32, 101, 101, 46, 102, 102, 32, 65, 83, 32, 102, 44, 40, 83, 69, 76, 69, 67, 84,
            32, 97, 32, 70, 82, 79, 77, 32, 96, 115, 99, 104, 101, 109, 97, 95, 98, 98, 96, 46, 96, 116, 98, 108, 95, 98,
            98, 96, 44, 40, 83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 99, 99, 99, 32, 65, 83, 32, 99, 44,
            32, 96, 100, 100, 100, 100, 96, 41, 41, 59};
    private final int len = srcBytes.length;

    /**
     * 基准测试执行
     * jvmArgs ： -XX:-TieredCompilation 关闭分层优化 -server, 使用之前要安装 JIT 编译代码的反汇编插件  HSDIS
     * -XX:+LogCompilation 运行之后项目路径会出现按照测试顺序输出hotspot_pid<PID>.log文件，
     * 根据最后运行的结果的顺序按文件时间找到对应的hotspot_pid<PID>.log文件, 然后使用 JITWatch 进行分析,
     * <p>
     * .addProfiler(CompilerProfiler.class)  // report JIT compiler profiling via standard MBeans
     * .addProfiler(GCProfiler.class)  // report GC time
     * .addProfiler(StackProfiler.class) // report method stack execution profile
     * .addProfiler(PausesProfiler.class)
     * You must install Windows Performance Toolkit. Once installed, locate directory with xperf.exe file
     * and either add it to PATH environment variable, or set it to jmh.perfasm.xperf.dir system property.
     * .addProfiler(WinPerfAsmProfiler.class)
     * 更多 Profiler, 请看 JMH 介绍
     * .output("BenchMarkDemo.log") 输出结果到文件
     *
     * @param args 入参
     * @throws RunnerException 异常信息
     */
    public static void main(String[] args) throws RunnerException {
        String s = "(¬(a) ∨ ((¬(b) ∧ (c ∨ d)) ∨ e))";
        System.out.println(s);
        Options opt = new OptionsBuilder()
                .include(MathematicalBenchMark.class.getSimpleName())
                .forks(1)
                .jvmArgs("-XX:+UnlockDiagnosticVMOptions",
                        "-XX:+LogCompilation",
                        "-XX:+TraceClassLoading",
                        "-XX:+PrintAssembly")
                .build();
        new Runner(opt).run();
    }


    //空循环 对照项
    @Benchmark
    public int emptyLoop() {
        int pos = 0;
        while (pos < len) {
            ++pos;
        }
        return pos;
    }

    @Benchmark
    public int increment() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            ++result;
            ++pos;
        }
        return result;
    }

    @Benchmark
    public int decrement() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            --result;
            ++pos;
        }
        return result;
    }

    @Benchmark
    public int ifElse() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            if (pos == 10) {
                ++result;
            }
            ++pos;
        }
        return result;
    }

    @Benchmark
    public int ifElse2() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            if (pos == 10) {
                ++result;
                ++pos;
            } else if (pos == 20) {
                ++result;
                ++pos;
            } else {
                ++pos;
            }
        }
        return result;
    }

    @Benchmark
    public int ifnotElse() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            if (pos == 10) {
                ++result;
            }
            ++pos;
        }
        return result;
    }

    @Benchmark
    public int ifLessThanElse() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            if (pos >= 10) {
                ++result;
            }
            ++pos;
        }
        return result;
    }

    @Benchmark
    public int ifGreaterThanElse() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            if (pos <= 10) {
                ++result;
            }
            ++pos;
        }
        return result;
    }

    @Benchmark
    public int readMemberVariable_a_byteArray() {
        int pos = 0;
        int result = 0;
        while (pos < len) {
            result = srcBytes[pos];
            pos++;
        }
        return result;
    }

}